import {getPNGAsync, getResumePdfAsync} from '@/http/api/resume';
import appStore from '@/store';

// 生成pdf方法
export const exportPdf = async (token?: string, id?: string, height?: string) => {
    const {resumeJsonNewStore} = appStore.useResumeJsonNewStore;
    const fileName = resumeJsonNewStore.title;
    const params = {
        url: `${location.origin}`,
        printBackground: true,
        timezone: '',
        margin: '',
        filename: '',
        bizType: 1,
        exportType: 1,
        height: height,
        draftId: id
    };
    const pdfData = await getResumePdfAsync(params);
    if (pdfData.code) {
        ElMessage.error('网络过慢，请求超时，请重新尝试导出');
        return;
    } else {
        const blob = new Blob([pdfData], {type: 'application/pdf'});
        const downloadElement = document.createElement('a');
        const href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = `${fileName}.pdf`; //下载后的文件名，根据需求定义
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
    }
};

// 生成PNG方法
export const exportPNG = async (token?: string, id?: string, height?: string) => {
    const {resumeJsonNewStore} = appStore.useResumeJsonNewStore;
    const fileName = resumeJsonNewStore.title;
    const params = {
        url: `${location.origin}`,
        bizType: 1,
        exportType: 2,
        height: height,
        draftId: id
    };
    const pdfData = await getPNGAsync(params);
    if (pdfData.code) {
        ElMessage.error('网络过慢，请求超时，请重新尝试导出');
        return;
    } else {
        const blob = new Blob([pdfData], {type: 'application/image'});
        const downloadElement = document.createElement('a');
        const href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = `${fileName}.png`; //下载后的文件名，根据需求定义
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
    }
};

export const getPdfUrl = (token?: string, id?: string, height?: string) => {
    return `${location.origin}/pdfPreview?token=${token}&&id=${id}&&height=${height}`;
};
